package com.spacex.danhuang.polive.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.spacex.danhuang.polive.Application.VApplication;
import com.spacex.danhuang.polive.R;

import org.json.JSONException;
import org.json.JSONObject;

import io.socket.client.Socket;
import io.socket.emitter.Emitter;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class AppBaseFragment extends Fragment {
    private static final String TAG = "AppBaseFragment";

    private VApplication app;
    private Socket mSocket;

    public AppBaseFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_app_base, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();

        app = (VApplication)getActivity().getApplication();
        mSocket = app.getSocket();
        mSocket.on("message", onHandleMsg);
    }

    @Override
    public void onPause() {
        super.onPause();

        mSocket.off("message", onHandleMsg);
    }

    /**
     * Socket事件监听处理函数（收到"message"事件时触发）
     **/
    protected Emitter.Listener onHandleMsg = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Log.i(TAG, "onHandleMsg received data");

            String onHandleMsgStr = (String)args[0];
            Log.i(TAG, onHandleMsgStr);
            JSONObject onHandleMsgJSON = null;
            try {
                onHandleMsgJSON = new JSONObject(onHandleMsgStr);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            String rsp_name = "";
            int rsp_status = 0;
            String rsp_errmsg = "";

            JSONObject onHandleMsgData = null;

            try {
                if (onHandleMsgJSON != null) {
                    rsp_name = onHandleMsgJSON.getString("response");
                    rsp_status = onHandleMsgJSON.getInt("status");
                    rsp_errmsg = onHandleMsgJSON.getString("errormsg");
                    Log.i(TAG, String.format("activity rsp_name = %s", rsp_name));
                    Log.i(TAG, String.format("rsp_status = %d", rsp_status));
                    Log.i(TAG, String.format("rsp_errmsg = %s", rsp_errmsg));

                    onHandleMsgData = onHandleMsgJSON.getJSONObject("data");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                return;
            }

            handleSwitchMethod(rsp_name, rsp_status, rsp_errmsg, onHandleMsgData);
        }
    };

    /**
     * 根据rsp_name进入每个activity中具体的业务数据处理函数
     * @param rsp_name Server端返回应答业务标志
     * @param rsp_status Server端应答状态 status=0为正常
     * @param rsp_errmsg Server端应答提示信息(一般为警告或错误信息)
     * @param onHandleMsgData Server端应答数据内容
     */
    protected abstract void handleSwitchMethod(String rsp_name, int rsp_status, String rsp_errmsg, JSONObject onHandleMsgData);
}
